package com.smiier.skin;

import java.util.HashMap;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.IntentCompat;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import cn.o.app.ui.OAlert;
import cn.o.app.ui.OAlert.OAlertListener;
import cn.skinapp.R;

import com.smiier.skin.constant.Constant;
import com.smiier.skin.intercept.RequestTaskIntercept;
import com.smiier.skin.ui.BasicActivity;
import com.smiier.skin.util.GlobalSettings;
import com.smiier.skin.util.ShareUtil;
import com.smiier.skin.utils.CommonUtility;

public class SystemActivity extends BasicActivity implements
		View.OnClickListener {

	RelativeLayout rl_update_password;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_system);
		init();
		TextView text_version_value = findViewById(R.id.text_version_value,
				TextView.class);
		text_version_value.setText("v"+CommonUtility.getVersion(this));
		text_version_value.setCompoundDrawables(null, null, null, null);
		setNavTitle("设置");
		setNavLeftBtn("我的");
		rl_update_password = findViewById(R.id.rl_update_password,
				RelativeLayout.class);
		if (GlobalSettings.getLoginType(this) == GlobalSettings.LOGIN_TYPE_THIRD) {
			rl_update_password.setVisibility(View.GONE);
		}
	}

	public void onClick(View view) {
		int id = view.getId();
		if (id == R.id.btn_left) {
			finish();
		} else if (id == R.id.btn_logout) {
			OAlert alert = new OAlert(getContext());
			alert.setOK("确认");
			alert.setCancel("取消");
			alert.setTitle("确定退出登陆吗？");
			alert.show();
			alert.setListener(new OAlertListener() {

				@Override
				public boolean onOK(OAlert alert) {
					GlobalSettings.setLoginType(getContext(),
							GlobalSettings.LOGIN_TYPE_UNKNOWN);
					GlobalSettings.setLoginUserPwd(getContext(), "");
					GlobalSettings.setLoginThirdId(getContext(), "");
					GlobalSettings.setLoginThirdType(getContext(), "");
					HashMap<String, Object> params = new HashMap<String, Object>();
					params.put(Constant.PARAM_TOKEN, GlobalSettings.sToken);
					params.put("GT_ClientID", "");
					params.put(Constant.PARAM_API, "User.Set");
					RequestTaskIntercept.requestIntercept(getContext(),
							Constant.URL.REQUEST, null, false, params);
					GlobalSettings.sUser = null;
					GlobalSettings.sUid = 0;

					Log.i("testtoken", "tuichu");

					GlobalSettings.sToken = null;
					CommonUtility.finish();

					Intent intent = null;
					if (CommonUtility.isDoctor()) {
						intent = new Intent(getContext(), LoginActivity.class);
					} else {
						intent = new Intent(getContext(), LoginActivity.class);
					}
					intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
							| IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
					startActivity(intent);
					return false;
				}

				@Override
				public boolean onCancel(OAlert alert) {
					return false;
				}
			});
		} else if (id == R.id.text_update_password) {
			try {
				String mobile = GlobalSettings.sUser.Mobile;
				CommonUtility.confirmSendCode(this, mobile,
						Constant.TYPE_UPDATE_PASSWORD);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else if (id == R.id.text_version_update) {
			checkVersion(false, new VersionUpdateListener() {

				@Override
				public void onYes() {

				}

				@Override
				public void onUpdateCancel() {

				}

				@Override
				public void onUpdate() {

				}

				@Override
				public void onNo() {
					toast("当前已经是最新版本。");
				}
			});
		} else if (id == R.id.text_about) {
			startActivity(new Intent(this, ShengmingActivity.class));
		} else if (id == R.id.itemview_tuijian) {
			String title = "蜜肤";
			String description = "还在为皮肤问题烦恼？大牌专家都在这里，去问问他们吧～";
			String url = "http://a.app.qq.com/o/simple.jsp?pkgname=com.pifuke.patient";
			// "http://www.pifupro.com/download.html";
			ShareUtil.share(this, url, title, description);
		} else if (id == R.id.itemview_about) {
			startActivity(new Intent(this, AboutUsActivity.class));
		}
	}

	/*
	 * @Override public void onResume() { super.onResume();
	 * MobclickAgent.onResume(getContext()); }
	 * 
	 * @Override public void onPause() { super.onPause();
	 * MobclickAgent.onPause(getContext()); }
	 */

}
